package com.huayi.website.controller.manage;

import com.huayi.website.entity.NewsEntity;
import com.huayi.website.request.GoodsAddVo;
import com.huayi.website.request.GoodsDeleteReq;
import com.huayi.website.service.NewsService;
import com.huayi.website.utils.EnumResultVo;
import com.huayi.website.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RequestMapping("/manage")
@Controller
public class MNewsController {
    @Autowired
    private NewsService newsService;

    /**
     * 列表
     *
     * @return
     */
    @RequestMapping(value = "/news_list", method = RequestMethod.GET)
    public String getList(HttpServletRequest request, Model model) {
        Integer categoryId = null;
        Integer pageIndex = 1, pageSize = 10;
        if (!StringUtils.isEmpty(request.getParameter("page"))) {
            pageIndex = Integer.parseInt(request.getParameter("page"));
        }
        if (!StringUtils.isEmpty(request.getParameter("categoryId"))) {
            categoryId = Integer.parseInt(request.getParameter("categoryId"));
            model.addAttribute("categoryId",categoryId);
        }
        String title="";
        if (!StringUtils.isEmpty(request.getParameter("title"))) {
            title = request.getParameter("title");
            model.addAttribute("title",title);
        }


        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("pageSize", pageSize);
        var result = newsService.getAllList(pageIndex, pageSize, title, categoryId,null);
        model.addAttribute("list", result.getList());
        model.addAttribute("totalSize", result.getTotalSize());

        var categoryList = newsService.getAllCategory();
        model.addAttribute("categoryList", categoryList);

        model.addAttribute("menu","news_list");
        return "manage/news_list";
    }

    /**
     * 添加新闻
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/news_add", method = RequestMethod.GET)
    public String add(HttpServletRequest request, Model model) {
        model.addAttribute("news",new NewsEntity());
        var categoryList = newsService.getAllCategory();
        model.addAttribute("categoryList", categoryList);

        model.addAttribute("menu","news_list");
        return "manage/news_add";
    }

    @RequestMapping(value = "/news_edit", method = RequestMethod.GET)
    public String edit(HttpServletRequest request, @RequestParam Integer id, Model model) {
        var news = newsService.getNewsById(id);
        model.addAttribute("news",news);
        var categoryList = newsService.getAllCategory();
        model.addAttribute("categoryList", categoryList);
        model.addAttribute("menu","news_list");
        return "manage/news_add";
    }

    @ResponseBody
    @RequestMapping(value = "/news_add_submit", method = RequestMethod.POST)
    public ResultVo<String> publishPost(Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String userName = session.getAttribute("userName").toString();
        if (StringUtils.isEmpty(userName)) return new ResultVo<>(500, "请登录");


        String id = request.getParameter("id");
        if(StringUtils.isEmpty(id)) id="0";
        String categoryId = request.getParameter("categoryId");
        String title = request.getParameter("title");
        String keyword = request.getParameter("keyword");
        String desc = request.getParameter("desc");
        String sort = request.getParameter("sort");
        String content = request.getParameter("content");
        Integer language = Integer.parseInt(request.getParameter("language"));


        //图片
        String image = request.getParameter("image");

        if (StringUtils.isEmpty(image)) {
            //商品主图不能为空
            return new ResultVo<>(200, "主图不能为空");
        }

        NewsEntity news = new NewsEntity();
        news.setCategoryId(Integer.parseInt(categoryId));
        news.setTitle(title);
        news.setKeyword(keyword);
        news.setDescription(desc);
        news.setImgUrl(image);
        news.setSort(Integer.parseInt(sort));
        news.setContent(content);
        news.setId(Integer.parseInt(id));
        news.setLanguage(language);

        Integer newsId = newsService.editNews(news);


        return new ResultVo<>(0, "SUCCESS");
    }

    /**
     * 删除
     *
     * @param req
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/news_delete", method = RequestMethod.POST)
    public ResultVo<Integer> delete(@RequestBody GoodsDeleteReq req) {
        if (req.getId() == null || req.getId() <= 0)
            return new ResultVo<>(EnumResultVo.ParamsError.getIndex(), "参数错误，缺少id");

        newsService.deleteNews(req.getId());

        return new ResultVo<>(EnumResultVo.SUCCESS);
    }


}
